﻿using System;
using System.Data;
using System.Windows.Forms;
using Relocation.Com.Tools;
using Relocation.Base;
using Relocation.UI;
using Relocation.Com;
using System.Diagnostics;
using Relocation.Base.UI;
using Relocation.Data;

namespace Relocation
{
	public partial class RelocateesHouseWindow : BaseForm
	{	
		public Relocatees_House RelocateHouse;
		public RelocateesHouseWindow()
			: base()
		{
			InitializeComponent();
			Initialize();
		}

		public RelocateesHouseWindow(Session session, Relocatees_House relocateHouse)
			: base(session)
		{
			InitializeComponent();
			Initialize();
			this.RelocateHouse = relocateHouse;
			if (!this.RelocateHouse.HouseReference.IsLoaded)
				this.RelocateHouse.HouseReference.Load();
			this.LoadHouse(relocateHouse.House);
			this.LoadRelocateHouse(this.RelocateHouse);
			
			this.Field_money1.Select();			

			if (this.RelocateHouse.House == null || string.IsNullOrEmpty(this.RelocateHouse.House.house_code))
			{
				this.Field_money1.Value = 0;				
				this.Field_money1.ReadOnly = true;				
			}
			if (this.RelocateHouse.House == null || string.IsNullOrEmpty(this.RelocateHouse.House.storeroom))
			{
				this.Field_money2.Value = 0;
				this.Field_money2.ReadOnly = true;
			}						
		}

		private void LoadRelocateHouse(Relocatees_House relocateHouse)
		{
			this.SetValueToInput(relocateHouse);						
		}

		protected override void SetValueToInput(object entity)
		{
			base.SetValueToInput(entity);			
			Relocatees_House relocatees_House = entity as Relocatees_House;
			if (relocatees_House != null)
			{
				this.Field_createdTimePicker.Value = relocatees_House.created;
				this.Field_updatedTimePicker.Text = relocatees_House.updated.ToString(null);
			}
		}


		private void LoadHouse(House house)
		{
			this.Field_House_HouseArea.Text = house.area1.ToDecimalString();
			this.Field_House_StoreArea.Text = house.area2.ToDecimalString();
			this.Field_House_HouseCode.Text = house.house_code;
			this.Field_House_StoreCode.Text = house.storeroom;
			this.Field_House_District.Text = house.district;
		}

		/// <summary>
		/// 保存
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		/// <remarks></remarks>
		private void save_Click(object sender, EventArgs e)
		{
			try
			{
				if (!this.ValidatorAllControls())
					return;
				this.GetValueFromInput(RelocateHouse);
				RelocateHouse.remark = this.Field_remark.Text.Trim();
				RelocateHouse.updated = DateTime.Now;

				this.Session.DataModel.Save(RelocateHouse);
				
				this.Session.GetMainTreeView().RefreshNode(RelocateHouse, EntityState.Modified);
				this.FieldIsChanged = false;
				this.DialogResult = DialogResult.OK;
				this.Close();
			} catch (Exception ex)
			{
				Log.Error(ex.GetInnerExceptionMessage());
				Debug.WriteLine(ex);
				MyMessagebox.Show("保存失败！");
			}
		}


		private void Initialize()
		{
			RoleInfo roleInfo = new RoleInfo(Session.KEY_ROLE_ADMIN, Session.KEY_ROLE_OPERATOR);
			this.Field_money1.Tag = new ControlTag("money1", new ValidatorInfo(false, ValidatorInfo.ValidatorTypes.Digit), null, roleInfo);
			this.Field_money2.Tag = new ControlTag("money2", new ValidatorInfo(false, ValidatorInfo.ValidatorTypes.Digit), null, roleInfo);
			this.Field_money3.Tag = new ControlTag("money3", new ValidatorInfo(false, ValidatorInfo.ValidatorTypes.Digit), null, roleInfo);
			this.Field_money4.Tag = new ControlTag("money4", new ValidatorInfo(false, ValidatorInfo.ValidatorTypes.Digit), null, roleInfo);
			this.Field_money5.Tag = new ControlTag("money5", new ValidatorInfo(false, ValidatorInfo.ValidatorTypes.Digit), null, roleInfo);
			this.Field_createdTimePicker.Tag = new ControlTag("created", null, null, roleInfo);
			this.Field_persons.Tag = new ControlTag("persons", new ValidatorInfo(new ValidatorInfo.ValidatorHandler(delegate(Control control) {
				if (!Validator.Required(control.Text))
				{
					control.Text = "0";
					return new ValidatorInfo.ValidatorResult(true);
				}
				//if (!Validator.Digit(control.Text))
				//        return new ValidatorInfo.ValidatorResult(false, "只能填写数字");
				//if (Convert.ToDecimal(control.Text) > people)				
				//    return new ValidatorInfo.ValidatorResult(false, "目前该户中尚未安置的人数为" + people + "人，您所输入的安置人数大于该数字，请重新输入！");
				//else
				return new ValidatorInfo.ValidatorResult(true);

			})), null, roleInfo);
			this.Field_remark.Tag = new ControlTag("remark", null, null, roleInfo);

		}

		private void button1_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}

	}
}
